<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<%@include file="/WEB-INF/taglibs/common/taglibs.jsp"%>

<head>
<title><spring:message code="admin.bonusreport.title" /></title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" href="<c:url value="/css/styles.css"/>" />
<script src="<c:url value="/javascripts/common.js"/>"></script>
<script language="javascript" type="text/javascript">
function updateDropdown(e,val){
	for(var i=0;i<e.length;i++)
	  {
   		 if(e[i].value == val)
   		   {e.selectedIndex = i;break;}
	  }
}

  function initForm(){
        updateDropdown(document.f1.bonusId,'<%=request.getParameter("bonusId")%>');
  }

  function selectUser(userId){
	  document.f1.userId.value = userId;
	  document.f1.submit();
  }

  function pay(bonusId , userId , payMode){
	  document.location.href = 'pay_bonus.htm?bonusId='+bonusId+'&userId='+userId +'&payMode='+payMode;
     }
</script>

</head>

<body><%@include file="../top_menu.jsp"%>
<table width=100%>
	<tr>
		<td><c:import url="/WEB-INF/jsp/menu/admin_bonus_menu.jsp" /><br />
		</td>
	</tr>
	<tr>
		<td><%@include file="/WEB-INF/jsp/include/messages.jsp"%>
		<br />
		</td>
	</tr>
</table>

<table width=70% class="formTable">

	<tr class="formHeader">
		<td colspan="5"><spring:message code="admin.bonusreport.label.bonusreport" /></td>
	</tr>
	<tr>
		<td colspan="2">
		<table>
			<tr>
				<td>
				<form name="f1" method="post"><select name="bonusId">
					<option value="1"><spring:message code="admin.bonusreport.label.selectbonusplan" /></option>
					<c:forEach items="${bonusList}" var="item" varStatus="i">
						<option value="${item.id}">${item.name}</option>
					</c:forEach>
				</select> <input type="hidden" name="userId"></input> <input type="button" class='ui-button ui-state-default ui-corner-all'
					value="<spring:message code="admin.bonusreport.button.view" />"
					onclick="if(this.form.bonusId.selectedIndex>0) this.form.submit();"></input>
				<c:if test="${fn:length(bonusList) <1}">
					<font color="red"><spring:message code="admin.bonusreport.label.nobonusplansetupmessage" />.</font>
				</c:if></form>
				</td>
			</tr>

			<c:if test="${userList != null}">
				<tr>
					<td><spring:message code="admin.bonusreport.label.selectuser" /> :<br />
					<c:forEach items="${userList}" var="u" varStatus="i">
						<a href="javascript:selectUser(${u.id});">${u.email}</a>
						<br />
					</c:forEach></td>
				</tr>
			</c:if>

			<c:if test="${bonus != null}">
				<tr>
					<td colspan="2">
					<table>
						<tr>
							<td colspan="2">
							<h3>${reportOwner.email} <c:if test="${canPay == true}">
								<input type="button" class='ui-button ui-state-default ui-corner-all' value="<spring:message code="admin.bonusreport.button.paynow" />"
									onclick="pay(${bonus.id},${reportOwner.id},'pay');"></input> | 
                        		<input type="button" class='ui-button ui-state-default ui-corner-all' value="<spring:message code="admin.bonusreport.button.payon" />"
									onclick="pay(${bonus.id},${reportOwner.id},'payOn');"></input> | 
                        		<input type="button" class='ui-button ui-state-default ui-corner-all' value="<spring:message code="admin.bonusreport.button.paywhen" />"
									onclick="pay(${bonus.id},${reportOwner.id},'payWhen');"></input>
							</c:if> <br />
							<spring:message code="admin.bonusreport.label.bonusplanidmessage" /> ${bonus.id} (${bonus.name})</h3>
							</td>
						</tr>
						<tr>
							<td colspan="2"><spring:message code="admin.bonusreport.label.yourplanmessage" />:</td>
						</tr>
						<c:if test="${bonus.methodId == 1}">
							<c:set var="amountSuffix" value="$"></c:set>
							<c:if test="${bonus.typeId == 2}">
								<c:set var="amountSuffix" value="%"></c:set>
							</c:if>

							<c:forEach items="${bonus.details.desiredAmounts}"
								var="desiredAmount" varStatus="i">
								<tr>
									<td>${bonus.columnName} ${bonus.operator} ${desiredAmount}
									you'll get <c:if test="${bonus.typeId == 1}"> ${bonus.details.flatRates[i.index]} </c:if>
									<c:if test="${bonus.typeId == 2}"> ${bonus.details.percentRates[i.index]} </c:if>
									${amountSuffix}</td>
								</tr>
							</c:forEach>

							<tr>
								<td colspan="2"><br />
								<spring:message code="admin.bonusreport.label.bonusearnedmessage" />:</td>
							</tr>
							<tr>
								<td colspan="2">${bonus.columnName} =
								${bonusResult.actualAmount} you earned
								$${bonusResult.calculatedAmount} (unpaid balance
								${bonusResult.unpaidBalance})</td>
							</tr>

							<c:forEach items="${bonus.bonusPayments}" var="p" varStatus="i">
								<tr>
									<td colspan="2"><a
										href="view_bonus_payment.htm?id=${p.id}">Payment #${p.id}</a></td>
								</tr>
							</c:forEach>



						</c:if>


						<c:if test="${bonus.methodId == 2}">

							<tr>
								<td>If you referred ${bonus.providerCount} or more service
								providers.<br />
								and each Service Provider refers ${bonus.customerCount} or more
								customer.<br />
								and each Customer has at least ${bonus.transactionCount}
								transactions<br />
								you'll get : ${bonus.payPerUnit} per ${bonus.payUnit}</td>
							</tr>


							<tr>
								<td colspan="2"><br />
								Here is what you have performed so far and the bonus earned:</td>
							</tr>
							<tr>
								<td colspan="2">
								<table border="1">
									<tr>
										<td><spring:message code="admin.bonusreport.label.serviceprovidername" /></td>
										<td><spring:message code="admin.bonusreport.label.serviceproviderid" /></td>
										<td><spring:message code="admin.bonusreport.label.numberofcustomers" /></td>
										<td><spring:message code="admin.bonusreport.label.totaltransactions" /></td>
										<td><spring:message code="admin.bonusreport.label.bonusearned" /></td>
										<td><spring:message code="admin.bonusreport.label.unpaidbalance" /></td>
									</tr>

									<c:set var="totalCustomers" value="0"></c:set>
									<c:set var="totalTransactions" value="0"></c:set>
									<c:set var="totalBonusEarned" value="0"></c:set>

									<c:forEach items="${bonusResult.details}" var="dtl"
										varStatus="i">
										<tr>
											<td>${dtl.serviceProviderName}</td>
											<td>${dtl.serviceProviderId}</td>
											<td>${dtl.customerCount}</td>
											<td>${dtl.transactionCount}</td>
											<td>${dtl.bonusEarned}</td>
											<td>N/A</td>

											<c:set var="totalCustomers"
												value="${totalCustomers+dtl.customerCount}"></c:set>
											<c:set var="totalTransactions"
												value="${totalTransactions+dtl.transactionCount}"></c:set>
											<c:set var="totalBonusEarned"
												value="${totalBonusEarned+dtl.bonusEarned}"></c:set>

										</tr>
									</c:forEach>

									<tr>
										<td colspan="2" align="center">Totals</td>
										<td>${totalCustomers}</td>
										<td>${totalTransactions}</td>
										<td>${totalBonusEarned}</td>
										<td>${bonusResult.unpaidBalance}</td>
									</tr>
								</table>
								</td>
							</tr>
							<c:forEach items="${bonus.bonusPayments}" var="p" varStatus="i">
								<tr>
									<td colspan="2"><a
										href="view_bonus_payment.htm?id=${p.id}">Payment #${p.id}</a></td>
								</tr>
							</c:forEach>



						</c:if>

					</table>
					</td>
				</tr>
			</c:if>
		</table>
		</td>
	</tr>


</table><%@include file="/WEB-INF/jsp/include/footer.jsp"%>
</body>
</html>